package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.FM;
import com.czh.springboot04.mapper.FMMapper;
import com.czh.springboot04.service.FMService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("fm")
public class FMController {
    @Autowired
    private FMService fMService;
    @PostMapping("/fmAdd")
    public R fmAdd(@RequestBody FM fm){
        int num = fMService.fmAdd(fm);
        if (num>0){
            return R.ok(num);
        }else {
            return R.fail("添加失败");
        }
    }
    @PutMapping("/fmUpdate")
    public R fmUpdate(@RequestBody FM fm){
        int num = fMService.fmUpdate(fm);
        if (num>0){
            return R.ok(num);
        }else {
            return R.fail("修改失败");
        }
    }
    @DeleteMapping("/fmDelete/{id}")
    public R fmDelete(@PathVariable int id){
        int num = fMService.fmDelete(id);
        if (num>0){
            return R.ok(num);
        }else {
            return R.fail("删除失败");
        }
    }
    @GetMapping ("/fmSelectAll")
    public R fmSelectAll(@RequestParam(required = false, defaultValue =  "1") int pageNum,
                         @RequestParam(required = false, defaultValue =  "5")int pageSize){
        PageInfo<FM> list  = fMService.fmSelectAll( pageNum,  pageSize);
        if (list!=null){
            return R.ok(list);
        }else {
            return R.fail("查询失败");
        }
    }
 @GetMapping("/fmSelectByMore")
    public R fmSelectByMore(@RequestParam(required = false, defaultValue =  "1") int pageNum,
                            @RequestParam(required = false, defaultValue =  "3")int pageSize){
        PageInfo<FM> pageInfo = fMService.fmByMoreTable(pageNum, pageSize);
        if (pageInfo!=null){
            return R.ok(pageInfo);
        }else {
            return R.fail("查询失败");
        }
    }
 @GetMapping("/fmSelectByName")
    public R fmSelectByName(@RequestParam(required = false, defaultValue =  "1") int pageNum,
                            @RequestParam(required = false, defaultValue =  "3")int pageSize,
                            @RequestParam(required = false, defaultValue =  "") String name){
        PageInfo<FM> pageInfo = fMService.fmSelectByName(pageNum, pageSize,name);
        if (pageInfo!=null){
            return R.ok(pageInfo);
        }else {
            return R.fail("查询失败");
        }
    }

}
